package com.jsoft.showcontacts;

import java.io.InputStream;
import java.util.LinkedList;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowContactActivity extends Activity {
	long contactId;
	TextView nameView;
	ImageView imageView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_contact);

		Intent intent = getIntent();
		contactId = intent.getExtras().getLong("contactId");

		nameView = (TextView) findViewById(R.id.contactName);
		imageView = (ImageView) findViewById(R.id.contactImage);

		QueryContactTask task = new QueryContactTask();
		task.execute(Long.valueOf(contactId));

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_show_contact, menu);
		return true;
	}

	class ContactData {
		boolean success;
		public String name;
		Bitmap image;
		LinkedList<String> phoneNumbers;
	}

	class QueryContactTask extends AsyncTask<Long, Void, ContactData> {

		@Override
		protected ContactData doInBackground(Long... params) {
			ContactData data = new ContactData();

			data.image = null;
			Uri contactUri = ContentUris.withAppendedId(
					ContactsContract.Contacts.CONTENT_URI,
					params[0].longValue());
			Cursor cursor = getContentResolver().query(contactUri,
					MainActivity.versionDependency.cursorProjection, null,
					null, null);
			if (cursor == null || !cursor.moveToFirst()) {
				data.success = false;
				return data;
			}
			data.name = cursor.getString(1);

			if (android.os.Build.VERSION.SDK_INT >= 11) {
				try {
					InputStream is = ContactsContract.Contacts
							.openContactPhotoInputStream(getContentResolver(),
									contactUri);
					data.image = BitmapFactory.decodeStream(is);
				} catch (Exception ex) {
				}
			} else {

				Uri photoUri = Uri.withAppendedPath(contactUri,
						ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);

				Cursor photoCursor = getContentResolver()
						.query(photoUri,
								new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO },
								null, null, null);
				if (photoCursor != null && photoCursor.moveToFirst()) {
					byte[] imageData = photoCursor.getBlob(0);
					data.image = BitmapFactory.decodeByteArray(imageData, 0,
							imageData.length);
				}
				if (photoCursor != null)
					photoCursor.close();
			}

			data.success = true;
			return data;
		}

		@Override
		protected void onPostExecute(ContactData result) {
			if (result.success) {
				nameView.setText(result.name);
				if (result.image != null)
					imageView.setImageBitmap(result.image);
			}
		}

	}

}
